package com.app.micaihu.view.army.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.app.base.BaseMvpFragment
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.micaihu.configure.LoadType
import com.app.micaihu.manager.UserInforManager
import com.app.micaihu.utils.StatServiceUtils
import com.app.micaihu.view.army.activity.ArmyDetailActivity
import com.app.micaihu.view.army.adapter.NoJoinedArmyListAdapter
import com.app.micaihu.view.army.contract.ArmyListContract
import com.app.micaihu.view.army.header.NoJoinedArmyHeader
import com.app.micaihu.view.army.presenter.ArmyListPresenter
import com.app.micaihu.view.bean.ArmyList
import com.app.utils.biz.Biz
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.view_common_rv.*

/**
 * 未加入军团
 */
class NotJoinedArmyListFragment : BaseMvpFragment<ArmyListContract.View, ArmyListPresenter>(), ArmyListContract.View {

    override fun getLayoutId() = R.layout.view_common_rv
    override fun getPresenter() = ArmyListPresenter()

    private val noJoinedArmyListAdapter: NoJoinedArmyListAdapter by lazy {
        val noJoinedArmyListAdapter = NoJoinedArmyListAdapter()
        rvList.layoutManager = LinearLayoutManager(context)

        noJoinedArmyListAdapter.headerWithEmptyEnable = true
        noJoinedArmyListAdapter.loadMoreModule.isAutoLoadMore = true
        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        noJoinedArmyListAdapter.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
        noJoinedArmyListAdapter
    }
    private val headerView by lazy {
        context?.let { NoJoinedArmyHeader(it) }
    }

    override fun setData() {
        rvList.adapter = noJoinedArmyListAdapter
    }

    override fun lazyLoad() {
        noJoinedArmyListAdapter.setEmptyView(loadingView)
        mPresenter?.refresh()
    }

    override fun addListeners() {
        refreshLayout.setOnRefreshListener {
            // 这里的作用是防止下拉刷新的时候还可以上拉加载
            noJoinedArmyListAdapter.loadMoreModule.isEnableLoadMore = false
            mPresenter?.refresh()
        }
        noJoinedArmyListAdapter.setOnItemClickListener { adapter, view, position ->
            StatServiceUtils.event(Config.EventBauduId.teamlistapply_click, "军团_申请列表点击")

            if (Biz.instance.uid().isEmpty()) {
                UserInforManager.getInstance().toLoginPage(activity)
                return@setOnItemClickListener
            }
            val item = noJoinedArmyListAdapter.getItem(position)
            val bundle = Bundle()
            bundle.putString(Config.Extra.id, item.gid)
            bundle.putString(Config.Extra.toUid, Biz.instance.uid())
            enterPager(ArmyDetailActivity::class.java, bundle)
        }
        noJoinedArmyListAdapter.loadMoreModule.setOnLoadMoreListener {
            mPresenter?.loadMore()
        }
    }

    fun refreshList() {
        mPresenter?.refresh()
        rvList?.let {
            rvList.smoothScrollToPosition(0)
        }
    }

    override fun handleArmyList(result: BaseBean<ArmyList?>?, loadType: Int) {
        refreshLayout.finishRefresh(Config.delayRefresh)
        noJoinedArmyListAdapter.loadMoreModule.isEnableLoadMore = true
        if (result == null) {
            if (noJoinedArmyListAdapter.data.isNotEmpty()) {
                if (LoadType.TYPE_REFRESH_ERROR == loadType) {
                    ToastUtils.showShort(R.string.empty_network_error)
                } else {
                    noJoinedArmyListAdapter.loadMoreModule.loadMoreFail()
                }
            } else {
                if (noJoinedArmyListAdapter.hasHeaderLayout()) {
                    headerView?.let { noJoinedArmyListAdapter.removeHeaderView(it) }
                }
                if (LoadType.TYPE_REFRESH_ERROR == loadType) {
                    noJoinedArmyListAdapter.setEmptyView(errorView)
                } else {
                    noJoinedArmyListAdapter.setEmptyView(getEmptyView())
                }
            }
        } else {
            if (result.isSuccess()) {
                result.data?.let {
                    if (!noJoinedArmyListAdapter.hasHeaderLayout()) {
                        headerView?.let { header -> noJoinedArmyListAdapter.addHeaderView(header) }
                    }
                    if (LoadType.TYPE_REFRESH_SUCCESS == loadType) {
                        noJoinedArmyListAdapter.setList(it.armyGroupList)
                        headerView?.setMyArmyData(it.myArmyGroup)
                    } else if (LoadType.TYPE_LOAD_MORE_SUCCESS == loadType) {
                        noJoinedArmyListAdapter.addData(it.armyGroupList)
                    }
                    if (it.armyGroupList.isNullOrEmpty()) {
                        //如果不够一页,显示没有更多数据布局
                        noJoinedArmyListAdapter.loadMoreModule.loadMoreEnd()
                    } else {
                        noJoinedArmyListAdapter.loadMoreModule.loadMoreComplete()
                    }
                }
            } else {
                ToastUtils.showShort(result.msg)
            }
        }

    }


}